<?php
namespace Home\Controller;
class SignController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("sign");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}


	//签约需求列表
	public function sign_list()
	{
		// p($_GET);
		$db_sign = M("sign s");
		$title = I('get.title');

		$where = " 1=1 ";
		if (!empty($title)) {$where .= " and (title like '%$title%' or media_name like '%$title%')";}
		$count = $db_sign->where($where)->join('__MEDIA__ m ON s.mid = m.id')->field('s.*,m.media_name')->count();
		$p = getpage($count, 20);
		$signlist = $db_sign->where($where)->join('__MEDIA__ m ON s.mid = m.id')->field('s.*,m.media_name')->limit($p->firstRow, $p->listRows)->order('id desc')->select();
		$page = $p->show();
		// 获取查询参数
		$map['title'] = I('get.title');
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($signlist);exit();
		$this->assign('signlist',$signlist);
		$this->assign('page', $page);
		$this->display();
	}

	//创建签约需求
	public function sign_add()
	{
		$medialist = M("media")->field('id,media_name')->select();
		$this->assign('medialist',$medialist);
		$this->today = date("Y-m-d",time());
		$this->display();
	}

	public function add_sign()
	{
		// p($_POST);exit();
		$mid = I('post.media');
		$title = I('post.title');
		$demand = I('post.demand');
		$number = I('post.number');
		$sex = I('post.sex');
		$deadline = I('post.deadline');
		if (empty($title)||empty($demand)||empty($mid)||empty($deadline)) {
			$this->error('参数错误',"",1);
		}

		$sign = D("sign");
		if (!$sign->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $sign->create(); // 保存生成的数据对象

			$data['mid'] = $mid;
			$data['title'] = $title;
			$data['demand'] = $demand;
			$data['number'] = $number;
			$data['sex'] = $sex;
			$data['deadline'] = strtotime($deadline." 23:59:59");
			$data['starttime'] = time();

			// p($data);exit();
			$result = $sign->add($data);
			$this->success('创建成功',"/Home/Sign/sign_list",1);
		}

	}

	//签约需求列表
	public function signjoin_list()
	{
		// p($_GET);
		$sid = I('get.sid');
		$signjoinlist =
		M('signjoin j')
			->join('__SIGN__ s ON j.sid = s.id')
			->join('__MEMBER__ m ON j.uuid = m.uuid')
			->join('__DATA__ d ON j.uuid = d.uuid')
			->join('__AUTHENTICATION__ a ON j.uuid = a.uuid')
			->where("j.sid = $sid AND m.status = 9")
			->field('s.mid,j.sid,j.id,j.time,m.username,a.name,m.uuid,d.describe_audio voiceurl')
			->select();
		// p($signjoinlist);exit();
		$this->assign('signjoinlist',$signjoinlist);
		$this->display();
	}

	public function sign_join()
	{
		// p($_POST);exit();
		$uuid = I('post.subscript');
		$sid = I('post.sid');
		$mid = I('post.mid');
		if (empty($uuid)||empty($sid)||empty($mid)) {
			$this->error('参数错误',"",1);
		}
		M("sign")->where(array('id'=>$sid))->save(array('type'=>1));//更新数据
		M("signjoin")->where(array('sid'=>$sid))->save(array('type'=>1));//更新数据
		M("media")->where(array('id'=>$mid))->save(array('uuid'=>$uuid));//更新数据
		$this->success('签约成功',"/Home/Sign/sign_list",1);
	}

}